// LPAtom.cpp: implementation of the LPAtom class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "lpmolecviewercontrol.h"
#include "LPAtom.h"
#include "windows.h"
//#include "string.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//Global variables

//Element names
//Not complete
string PeriodicTableType[]=
		{"H", "He", 
         "Li", "Be", "B", "C", "N", "O", "F", "Ne", 
         "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", 
         "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", 
         "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe"
         };

//Radius of the spheres shown
//http://en.wikipedia.org/wiki/Atomic_radii_of_the_elements_(data_page)
//Need to define which radius it should be using
double	PeriodicTableRadius[]=
		{0.37, 0.32
        , 1.34, 0.90, 0.82, 0.77, 0.75, 0.73, 0.71, 0.69
        , 1.54, 1.30, 1.18, 1.11, 1.06, 1.02, 0.99, 0.97
        , 1.96, 1.74, 1.44, 1.36, 1.25, 1.27, 1.39, 1.25, 1.26, 1.21, 1.38, 1.31, 1.26, 1.22, 1.19, 1.16, 1.14, 1.10
        ,0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 1.44, 0.91, 0.91, 0.91, 0.91, 0.91, 1.32, 0.91
        
		};

//COLORREF is 32 bit (DWORD) BRG (not RGB color)
//http://msdn.microsoft.com/en-us/library/ms532655(VS.85).aspx
//Color in this table is written in RGB.
//When setting color in colour[] it is stored properly.
DWORD PeriodicTableColor[]=
        {0x00FFFFFF,		//1		H
         0x00D9FFFF,		//2		He
         0x00CC80FF,		//3		Li
         0x00C2FF00,		//4		Be
         0x00FFB5B5,		//5		B
         0x00909090,		//6		C
         0x003050F8,		//7		N
         0x00FF0D0D,		//8		O
         0x0090E050,		//9		F
         0x00B3E3F5,		//10	Ne
         0x00AB5CF2,		//11	Na
         0x008AFF00,		//12	Mg
         0x00BFA6A6,		//13	Al
         0x00F0C8A0,		//14	Si
         0x00FF8000,		//15	P
         0x00FFFF30,		//16	S
         0x001FF01F,		//17	Cl
         0x0080D1E3,		//18	Ar
         0x008F40D4,		//19
         0x003DFF00,		//20
         0x00E6E6E6,		//21
         0x00BFC2C7,		//22
         0x00A6A6AB,		//23
         0x008A99C7,		//24
         0x009C7AC7,		//25
         0x00E06633,		//26
         0x00F090A0,		//27
         0x0050D050,		//28
         0x00C88033,		//29
         0x007D80B0,		//30
         0x00C28F8F,		//31
         0x00668F8F,		//32
         0x00BD80E3,		//33
         0x00FFA100,		//34
         0x00A62929,		//35
         0x005CB8D1,		//36		Kr
         0x00702EB0, 
         0x0000FF00,		//38 
         0x0094FFFF, 
         0x0094E0E0,		//40 
         0x0073C2C9, 
         0x0054B5B5, 
         0x003B9E9E, 
         0x00248F8F, 
         0x000A7D8C, 
         0x00006985, 
         0x00C0C0C0,		//47		Ag
         0x00FFD98F, 
         0x00A67573, 
         0x00668080,		//50
         0x009E63B5, 
         0x00D47A00, 
         0x00940094, 
         0x00429EB0,		//54		Xe
         0x0057178F,		//55		Cs
         0x0000C900, 
         0x0070D4FF, 
         0x00FFFFC7, 
         0x00D9FFC7, 
         0x00C7FFC7, 
         0x00A3FFC7, 
         0x008FFFC7, 
         0x0061FFC7, 
         0x0045FFC7, 
         0x0030FFC7, 
         0x001FFFC7, 
         0x0000FF9C, 
         0x0000E675, 
         0x0000D452, 
         0x0000BF38, 
         0x0000AB24, 
         0x004DC2FF, 
         0x004DA6FF, 
         0x002194D6, 
         0x00267DAB, 
         0x00266696, 
         0x00175487, 
         0x00D0D0E0, 
         0x00FFD123, 
         0x00B8B8D0, 
         0x00A6544D, 
         0x00575961, 
         0x009E4FB5, 
         0x00AB5C00, 
         0x00754F45, 
         0x00428296, 
         0x00420066, 
         0x00007D00, 
         0x0070ABFA, 
         0x0000BAFF, 
         0x0000A1FF, 
         0x00008FFF, 
         0x000080FF, 
         0x00006BFF, 
         0x00545CF2, 
         0x00785CE3, 
         0x008A4FE3, 
         0x00A136D4, 
         0x00B31FD4, 
         0x00B31FBA, 
         0x00B30DA6, 
         0x00BD0D87, 
         0x00C70066, 
         0x00CC0059, 
         0x00D1004F, 
         0x00D90045, 
         0x00E00038, 
         0x00E6002E,
         0x00EB0026
	};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LPAtom::LPAtom()
{
	colour[0]=255;colour[1]=255;colour[2]=255;
	elemno=1; //hydrogen default element
	serno=0;
	strcpy(type,"H");
	x=0; //Positioned at origin
	y=0;
	z=0;


}

LPAtom::~LPAtom()
{
	//delete[] type;
	//delete[] colour;

}
void LPAtom::setType(char *type1)
{
	int el;
	strncpy(type,type1,sizeof(type));	//Copy type
	el=this->getElemNumber(this->type); //Gets element number
	setElemNumber(el);					//Sets element number, updates other values
}

int LPAtom::getElemNumber(char *type1)
{
	int el=0; //Initialises element number, zero unknown
	
	//Makes a local copy of the element type
	//Get the size of the string passed
	//int len=strlen(type1);
	//char a[100];
	//strncpy(a,type1,len);
	
	char *pdest;
	char pertemp[10];

	int numberelements=sizeof(PeriodicTableType)/sizeof(PeriodicTableType[0]);

	for (int i=0; i<numberelements;i++){
		strncpy(pertemp,PeriodicTableType[i].c_str(),sizeof(pertemp));
		pdest=strstr(type1,pertemp); //Gets first occurence of string, NULLS if not found
		if (pdest!=NULL){
			el=i+1;						//Sets element number but continues searching for other matches
			if (strlen(pertemp)>1) break; //If last match was gave a string with 2 characters then must be exact match
		}
	}


	return el;
}

void LPAtom::setElemNumber(int el)
{
	//Sets element number in this class and updates coulor and radius
	//Radius is the lattice constant value
	elemno=el; //Sets element number

	//Default values
	colour[0]=255;
	colour[1]=20;
	colour[2]=147;
	radius=0.91;
	
	int n;

	// set colors
	n=sizeof(PeriodicTableColor)/sizeof(PeriodicTableColor[0]);
	if (elemno>0 && elemno<=n){
		
		//Note inversion from COLORREF DWORD (BRG) to RGB
		colour[0]=GetBValue(PeriodicTableColor[elemno-1]);
		colour[1]=GetGValue(PeriodicTableColor[elemno-1]);
		colour[2]=GetRValue(PeriodicTableColor[elemno-1]);

	}

	n=sizeof(PeriodicTableRadius)/sizeof(PeriodicTableRadius[0]);
	if (elemno>0 && elemno<=n){
		//Set radius
		radius=PeriodicTableRadius[elemno-1]; //radii=use carbon value
	}


}
